/**
 * VYPe11 Project
 * VYPe11 language compiler.
 * Authors: Jozef Lang (xlangj01), 
 * 			Zoltan Zemko (xzemko01)
 * 2011
 */
package cz.vutbr.fit.VYPe.team18.VYPe11.nodes.expression.unary;

import java.io.IOException;

import cz.vutbr.fit.VYPe.team18.VYPe11.exception.SematicException;
import cz.vutbr.fit.VYPe.team18.VYPe11.nodes.data_type.DataType;
import cz.vutbr.fit.VYPe.team18.VYPe11.nodes.expression.ExpressionNode;

public class UnaryNode extends UnaryExpressionNode {
	public enum UnaryMode {
		COMPLEMENT,
		NOT
	};
	
	private UnaryMode unaryMode;
	
	public UnaryNode(ExpressionNode unaryExpr,
							UnaryMode unaryMode) {
		this.expression=unaryExpr;
		this.unaryMode=unaryMode;
	}

	public UnaryMode getUnaryMode() {
		return unaryMode;
	}
	
	public DataType getType() throws SematicException {
		if(expressionType!=null)
			return expressionType;
		
		DataType dt=this.expression.getType();
		if(dt==DataType.INT || dt==DataType.SHORT)
		{
			expressionType=dt;
		}
		else
		{
			throw new SematicException("Equality operands need to be same type.");
		}
		return expressionType;
	}

	@Override
	public void generateTAC() throws IOException {
		expression.generateTAC();
		int i=tacCounter.getTermIndex()-1;
		String operation="~";
		if(unaryMode==UnaryMode.NOT)
			operation="!";
		writeTAC(operation, "_t"+i, "", "_t"+tacCounter.getNextTermIndex());
	}
}
